﻿var mediate = {
    init: function() {
        var that = this;
        that.initList(1);
        that.initApply();
        that.initUploadImg();
        that.doSubmit();
    },

    initList: function(page) {
        var that = this;
        $.ajax({
            url: COMMONURL + "api/BidDispute/GetBidList?token=" + $.cookie('token'),
            type: "post",
            dataType: "json",
            data: {
                page: page,
                pagesize: 20
            }
        }).done(function(redata) {
            if (redata.recode !== '0000') return;
            if (!redata.mod) return;
            if (redata.mod.DataObj.length <= 0) return;
            var odata = redata.mod.DataObj;
            //alert(that.format(odata[0].end_bid_time));
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                }
            var compiled = _.template($("#mediateTpl").html());
            $("#mediate").html(compiled(redata.mod.DataObj));
            // 显示分页
            $("#recodePage").show().jqPaginator({
                //totalPages: Math.ceil(redata.mod.TotalNum/20),
                totalPages: redata.mod.TotalPageCount,
                visiblePages: 20,
                currentPage: page,
                first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                onPageChange: function(num, type) {
                    if (type == 'change') {
                        that.initList(num);
                    }
                }
            });
        })
    },
    /**
     * 初始化申请按钮
     */
    initApply: function() {
        $('#mediate').on('click', '.J_apply', function() {
            // 初始化表单
            var bidNo = $(this).closest('[data-bid-no]').data('bid-no');
            var bidId = $(this).closest('[data-bid-id]').data('bid-id');
            $('[name=bid_no]').val(bidNo);
            $('[name=bid_id]').val(bidId);
            $('[name=info]').val('');
            $('[name=contact]').val('');
            $('[name=phone]').val('');
            $('[name=photo]').val('');
            $('[name=type]').val(0);
            $('.pre-img').attr('src', '');
            // 显示窗口
            $('#mediateModal').modal();
        });
    },
     format: function(nS) {
        //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
        if (!nS) return null;
        var date = new Date(nS * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var d = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        return year + '-' + month + '-' + d + ' ' + hours + ':' + minutes;
    },
    initUploadImg: function() {
        /**
         * 将图片数据上传
         */
        function uploadData() {

            var data = new FormData();

            var files = $("#mediatePhone").get(0).files;
            // alert(files);
            // Add the uploaded image content to the form data collection
            if (files.length > 0) {
                data.append("UploadedImage", files[0]);
            }

            // Make Ajax request with the contentType = false, and procesDate = false
            var ajaxRequest = $.ajax({
                type: "POST",
                url: "http://api.qy.7yunbao.com/api/fileupload/uploadfile",
                contentType: false,
                processData: false,
                data: data
            });

            ajaxRequest.done(function(responseData, textStatus) {
                if (textStatus == 'success') {
                    if (responseData != null) {
                        if (responseData.Key) {
                            // alert('图片上传成功');
                            $('.pre-img').attr('src', responseData.Value);
                            // preview();
                            $("#mediatePhone").val('');
                            // 将图片的url记录到表单
                            $('[name=photo]').val(responseData.Value);
                        } else {
                            alert(responseData.Value);
                        }
                    }
                } else {
                    alert(responseData.Value);
                }
            });
        }

        $('#mediatePhone').on('change', function() {
            uploadData();
        })

    },
    doSubmit: function() {
        var that = this;
        // 添加重复密码的规则
        jQuery.validator.addMethod("phone", function(value, element) {
            if (that.checkPhone(value))
                return true;
            else
                return false;
        }, '请输入正确的手机号码');
        $("#mediateApplyForm").validate({
            rules: {
                bid_no: "required",
                type: 'required',
                info: 'required',
                contact: 'required',
                phone: 'required phone',
            },
            messages: {
                type: '请选择纠纷类型',
                info: '请输入问题描述',
                contact: '请输入联系人',
                phone: '请输入手机号',
            },
            submitHandler: function(form) {
                $.ajax({
                    url: COMMONURL + "api/BidDispute/Apply?token=" + $.cookie('token'),
                    type: "post",
                    dataType: "json",
                    data: {
                        bid_id: $('[name=bid_id]').val(),
                        bid_no: $('[name=bid_no]').val(),
                        info: $('[name=info]').val(),
                        contact: $('[name=contact]').val(),
                        phone: $('[name=phone]').val(),
                        photo1: $('[name=photo]').val(),
                        type: $('[name=type]').val(),

                    }
                }).done(function(data) {
                    if (data.recode == 0000) {
                        alert(data.resmessage);
                        // 关闭弹窗
                        $('#mediateModal').modal('hide');
                    } else {
                        alert(data.resmessage);
                    }
                })
            }
        })
    },
    // 验证手机号码
    checkPhone: function(phone) {
        return /^1[34589]\d{9}$/.test(phone);
    },
}

mediate.init();
